package me.numark.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.Bind;
import me.numark.R;
import me.numark.fragments.BaseListFragment;
import me.numark.fragments.FeedFragment;
import me.numark.utils.Constants;

/**
 * Created by Evan on 15/8/10.
 */
public class TagActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {

    @Bind(R.id.viewpager)
    public ViewPager pager;

    @Bind(R.id.radio_left)
    RadioButton leftRadio;

    @Bind(R.id.radio_right)
    RadioButton rightRadio;

    @Bind(R.id.radio_group)
    RadioGroup radioGroup;

    BaseListFragment[] fragments = new BaseListFragment[2];
    String tag;
    public static final String EXTRA_TAG = "tag";

    TagPageAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_tag;
        super.onCreate(savedInstanceState);
        tag = getIntent().getStringExtra(EXTRA_TAG);
        titleTextView.setText(tag);
        //titleTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.white_tag, 0, 0, 0);
        mAdapter = new TagPageAdapter();
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        onPageSelected(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radio_right) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        if (i == 1) {
            radioGroup.check(R.id.radio_right);
        } else {
            radioGroup.check(R.id.radio_left);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    private class TagPageAdapter extends FragmentPagerAdapter {

        public TagPageAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int i) {
            if (fragments[i] == null) {
                fragments[i] = FeedFragment.newInstance(i == 0 ? Constants.FEEDTYPE_TAG_LAST : Constants.FEEDTYPE_TAG_HOT,
                        tag);
            }
            return fragments[i];
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}
